<?php
namespace app\common\logic;

use think\Db;

class Article extends Base
{

    // 获取数量列表通过月份
    public function getNumListByMonth()
    {
        $rt = Db::query("select from_unixtime(create_time, '%Y年%m月') as `name`,from_unixtime(create_time, '%Y') as `year`,from_unixtime(create_time, '%m') as `month`,count(*) as num,CONCAT('/index/article/month/',from_unixtime(create_time, '%Y'),'/',from_unixtime(create_time, '%m')) as url from article group by `name` order by create_time desc");
        return $rt;
    }

    // 获取数量列表通过标签
    public function getNumListCategory()
    {
        $names = Db::name('article_sort')->select();
        $names = array_column($names, 'name', 'id');
        $list = Db::name('article')->field('sort_id,count(*) as num')->group('sort_id')->select();
        foreach ($list as &$item) {
            $item['name'] = $names[$item['sort_id']];
            $item['url'] = '/index/article/category/'.$item['sort_id'];
        }
        return $list;
    }

    public function category()
    {
        $rt = Db::name('article')->field('sort_id,count(*) as num')->group('sort_id')->select();
        return $rt;
    }

    // 获取月份归档的html
    public function getMonthHtml()
    {
        $data = $this->getNumListByMonth();
        $str = '';
        foreach ($data as $item) {
            $url = '/index/article/month/'.$item['year'].'/'.$item['month'];
            $str .= <<<html
                            <li>
                                <a href="{$url}">{$item['name']}
                                    <span class="float-right">{$item['num']}篇</span>
                                </a>
                            </li>
html;
        }
        return $str;
    }
}